package com.jackeysun.common_kit

import android.content.Context
import android.content.res.Resources
import android.graphics.*
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.jackeysun.common_kit.databinding.ActivityMainBinding
import com.jackeysun.util.ToastExt.mApplication
import com.jackeysun.util.ToastExt.toast

import android.util.Base64
import com.jackeysun.util.dp
import android.graphics.Bitmap
import com.jackeysun.util.px


class MainActivity : AppCompatActivity() {

    private var mBinding: ActivityMainBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding?.root)
        mApplication = this.application

        val viewWidth: Int = windowManager.defaultDisplay.width - dp2px(this, 20f)
        mBinding?.tv?.initWidth(viewWidth)
        mBinding?.tv?.setHasAnimation(true)
//        mBinding?.tv?.setCloseInNewLine(true)
        mBinding?.tv?.setOpenSuffixColor(Color.RED)
        mBinding?.tv?.setCloseSuffixColor(Color.RED)
        mBinding?.tv?.setOriginalText(
            """
                在全球，随着Flutter被越来越多的知名公司应用在自己的商业APP中，Flutter这门新技术也逐渐进入了移动开发者的视野，尤其是当Google在2018年IO大会上发布了第一个Preview版本后，国内刮起来一股学习Flutter的热潮。
                为了更好的方便帮助中国开发者了解这门新技术，我们，Flutter中文网，前后发起了Flutter翻译计划、Flutter开源计划，前者主要的任务是翻译Flutter官方文档，后者则主要是开发一些常用的包来丰富Flutter生态，帮助开发者提高开发效率。而时至今日，这两件事取得的效果还都不错！
                """.trimIndent()
        )
//        Toast("gg")
//        this Toast "gg"
//
//        ToastMiddle("gg")
//
//        ToastSingle("gg")

        mBinding?.toast?.setOnClickListener {
//            this@MainActivity.toast("gg")
            "gg".toast()
        }

        mBinding?.btn?.setProgress(progress = 30)


        val str =
            ""
//        val decodedString = Base64.decode(str.split(",")[1], Base64.DEFAULT)
//        val bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)

        val width = Resources.getSystem().displayMetrics.widthPixels - 400.px()
        val decodedString = Base64.decode(str.split(",")[1], Base64.DEFAULT)
        val bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
        val scale = (width.toFloat()/bitmap.width)
        mBinding?.iv?.setImageBitmap(scaleBitmap(bitmap, scale))
    }

    private fun scaleBitmap(origin: Bitmap?, ratio: Float): Bitmap? {
        if (origin == null) {
            return null
        }
        val width = origin.width
        val height = origin.height
        val matrix = Matrix()
        matrix.preScale(ratio, ratio)
        val newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false)
        if (newBM == origin) {
            return newBM
        }
        origin.recycle()
        return newBM
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    fun dp2px(context: Context, dpValue: Float): Int {
        var res = 0
        val scale: Float = context.getResources().getDisplayMetrics().density
        res =
            if (dpValue < 0) (-(-dpValue * scale + 0.5f)).toInt() else (dpValue * scale + 0.5f).toInt()
        return res
    }
}